package com.ztom.beans.factory.support.instantiate

import com.ztom.beans.exception.BeansException
import com.ztom.beans.factory.support.pojo.BeanDefinition
import kotlin.reflect.full.createInstance

/**
 * @author ZhangTao
 */
class SimpleInstantiationStrategy : InstantiationStrategy {

    override fun instantiate(beanDefinition: BeanDefinition): Any {
        try {
            println("使用空构造方法创建 bean: [${beanDefinition.beanClazz.simpleName}]")
//            val constructor = beanDefinition.beanClazz.java.getDeclaredConstructor()
//            return constructor.newInstance()
            return beanDefinition.beanClazz.createInstance()
        } catch (e: Exception) {
            throw BeansException("Failed to instantiate [${beanDefinition.beanClazz.qualifiedName}]", e)
        }
    }
}